package com.wechat.page.contact;

import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UIStackPane;
import com.swak.ui.Event;
import com.swak.ui.EventListener;
import com.wechat.Cons;
import com.wechat.dto.ContactDTO;
import com.wechat.page.comps.TitleComp;
import com.wechat.page.contact.profile.ContactProfilePage;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

/**
 * ContactListPage.
 * 
 * @author 618lf
 */
@FXMLView(title = "联系人详细", value = "ContactContentPage.fxml", css = "ContactContentPage.css")
public class ContactContentPage extends AbstractPage implements EventListener {

	private TitleComp titlePage;
	private UIIcon chatIcon;

	@FXML
	private UIStackPane contentPane;

	public ContactContentPage() {
		this.titlePage = this.NewComp(TitleComp.class);
		this.chatIcon = new UIIcon(UI.Icons.COMMENTS);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		Node titlePane = titlePage.getRoot();
		this.addChild(titlePane);
		GridPane.setConstraints(titlePane, 0, 0);
		GridPane.setHgrow(titlePane, Priority.ALWAYS);

		this.chatIcon.Size(88);
		this.chatIcon.setOpacity(0.2);
		this.contentPane.getChildren().add(this.chatIcon);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.activeProperty().addListener(this::onContactActive);
	}

	private void onContactActive(ObservableValue<? extends ContactDTO> ob, ContactDTO unActive, ContactDTO active) {
		this.$runUI(() -> {
			if (active != null) {
				ContactProfilePage page = this.NewPage(ContactProfilePage.class);
				this.contentPane.getChildren().remove(0);
				this.contentPane.getChildren().add(page.getRoot());
				page.contactProperty().set(active);
			} else {
				this.contentPane.getChildren().remove(0);
				this.contentPane.getChildren().add(chatIcon);
			}
		});
	}

	// ================================================================================
	// 事件监听
	// ================================================================================
	@Override
	public void listen(Event event) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("[联系人面板] - 收到事件 ：{}", event);
		}

		if (Cons.Event_Contact_Select.equals(event.getKey())) {
			ContactDTO select = event.getMessage();
			this.activeProperty().set(select);
		} else if (Cons.Event_Contact_Refresh.equals(event.getKey())) {
			ContactDTO select = event.getMessage();
			this.activeProperty().set(select);
		}
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private final ObjectProperty<ContactDTO> activeProperty = new SimpleObjectProperty<>();

	public ObjectProperty<ContactDTO> activeProperty() {
		return activeProperty;
	}

}
